package com.baosight.sczx.zm.cm.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cm.domain.ZMCMExportApplicationDetail;

public class ServiceZMCM84  extends ServiceEPBase {
	/**
	 * 初始数据管理从表页面初始化方法
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM81</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 下午01:35:45  张坤  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) 
	{
		EiInfo outInfo;
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的
		outInfo = this.query(inInfo);
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.units"));
		return outInfo;
	}
	
	/**
	 * 查询方法
	 * 根据查询条件查询从表数据信息
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM81</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 下午01:36:45  张坤  Create
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) 
	{
		String fdApplicationId = inInfo.getString("fdApplicationId");
		StaticTools.setQueryParameters(inInfo, "fdApplicationId", fdApplicationId, true);//添加查询条件
		EiInfo outInfo = super.query(inInfo, "ZMCMExportApplicationDetail.query", new ZMCMExportApplicationDetail());
		return outInfo;
	}
	
	/**
	 * 修改方法
	 * 修改从表数据
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCM81</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2010-12-17 下午01:43:45  张坤  Create
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@Override
	public EiInfo update(EiInfo inInfo) 
	{
		String fdApplicationNumber = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdApplicationNumber");
		String fdAuditNumber = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdAuditNumber");
		if((Double.parseDouble(fdAuditNumber) - Double.parseDouble(fdApplicationNumber)) > 0)
		{
			inInfo.setStatus(-1);
			inInfo.addMsg("审批数量不能大于申请数量！");
			return inInfo;
		}
		
		String fdApplicationId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdApplicationId");
		updateTeamPlan(inInfo);
		
		Double double_fdApplicationSum = 0.0;
		EiInfo outInfo = super.update(inInfo, "ZMCMExportApplicationDetail.update");//更新从表数据
		
		Map inMap = new HashMap();
		inMap.put("fdApplicationId", fdApplicationId);
		List _list = dao.query("ZMCMExportApplicationDetail.queryStrict", inMap);
		
		for(int i=0; i<_list.size(); i++)
		{
			ZMCMExportApplicationDetail detail = (ZMCMExportApplicationDetail)_list.get(i);
			Double double_fdAuditNumber = detail.getFdAuditNumber();//获取个数
			Double double_fdPrice =  detail.getFdPrice();//获取价格
			double_fdApplicationSum = double_fdApplicationSum + double_fdAuditNumber * double_fdPrice;
		}
		
		inMap = new HashMap();
		inMap.put("fdApplicationSum", BigDecimal.valueOf(double_fdApplicationSum));
		inMap.put("fdApplicationId", fdApplicationId);
		dao.update("ZMCMExportApplication.applicationSum", inMap);//更新主表金额

		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: updateTeamPlan</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2011-12-2上午03:26:13 严洪浩
	 * </p>
	 *
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	private void updateTeamPlan(EiInfo inInfo)
	{
		String fdKeyId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdKeyId");
		String fdApplicationId = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdApplicationId");
		String fdAuditNumber = inInfo.getBlock(EiConstant.resultBlock).getCellStr(0, "fdAuditNumber");
		
		Map inMap = new HashMap();
		inMap.put("fdApplicationId", fdApplicationId);
		List _list = dao.query("ZMCMExportApplicationDetail.queryStrict", inMap);
		
		for(int i=0; i<_list.size(); i++)
		{
			ZMCMExportApplicationDetail md = (ZMCMExportApplicationDetail)_list.get(i);
			
			if(fdKeyId.equals(md.getFdKeyId()))
			{
				inMap = new HashMap();
				inMap.put("fdApplicationNumber", 0-md.getFdAuditNumber()+Double.parseDouble(fdAuditNumber));
				inMap.put("fdRequirePlan", md.getFdRequirePlan());
						
				dao.update("ZMCMTeammaterialplanDetail.updatefdApplicationExportNumForNoCode", inMap);
				
				if(Double.parseDouble(fdAuditNumber)<md.getFdAuditNumber())
				{
					inMap.put("fdStatus", "部分领用");
					dao.update("ZMCMTeammaterialplanDetail.updatefdStatusForNoCode", inMap);
				}
				
				break;
			}
		}
	}
}